﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.ComponentModel;

namespace WickedFlame.VecDraw.Base.Controls
{
	public enum ColorSelection { Default, Secondary };

	[TemplatePart(Name = "PART_SecondaryButton", Type = typeof(Button)), TemplatePart(Name = "PART_PrimaryButton", Type = typeof(Button))]
	public class ColorSwitch : System.Windows.Controls.Control
	{
		Button secondaryButton;
		Button primaryButton;

		static ColorSwitch()
        {
			DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorSwitch), new FrameworkPropertyMetadata(typeof(ColorSwitch)));
        }

		public ColorSwitch()
		{
		}

		public override void OnApplyTemplate()
		{
			if (secondaryButton != null)
			{
				secondaryButton.Click -= new RoutedEventHandler(secondaryButton_Click);
				secondaryButton = null;
			}
			if (primaryButton != null)
			{
				primaryButton.Click -= new RoutedEventHandler(primaryButton_Click);
				primaryButton = null;
			}

			base.OnApplyTemplate();

			secondaryButton = GetTemplateChild("PART_SecondaryButton") as Button;
			if (secondaryButton != null)
				secondaryButton.Click += new RoutedEventHandler(secondaryButton_Click);

			primaryButton = GetTemplateChild("PART_PrimaryButton") as Button;
			if (primaryButton != null)
				primaryButton.Click += new RoutedEventHandler(primaryButton_Click);
		}

		void secondaryButton_Click(object sender, RoutedEventArgs e)
		{
			IsPrimarySelected = false;
		}

		void primaryButton_Click(object sender, RoutedEventArgs e)
		{
			IsPrimarySelected = true;
		}

		[Bindable(true)]
		public Color SelectedFillColor
		{
			get
			{
				return (Color)GetValue(SelectedFillColorProperty);
			}
			set
			{
				SetValue(SelectedFillColorProperty, value);
			}
		}

		public static readonly DependencyProperty SelectedFillColorProperty =
			DependencyProperty.Register("SelectedFillColor", typeof(Color), typeof(ColorSwitch), new PropertyMetadata(Colors.Transparent));

		[Bindable(true)]
		public Color SelectedStrokeColor
		{
			get
			{
				return (Color)GetValue(SelectedStrokeColorProperty);
			}
			set
			{
				SetValue(SelectedStrokeColorProperty, value);
			}
		}

		public static readonly DependencyProperty SelectedStrokeColorProperty =
			DependencyProperty.Register("SelectedStrokeColor", typeof(Color), typeof(ColorSwitch), new PropertyMetadata(Colors.Transparent));

		[Bindable(true)]
		public bool IsPrimarySelected
		{
			get
			{
				return (bool)GetValue(IsPrimarySelectedProperty);
			}
			set
			{
				SetValue(IsPrimarySelectedProperty, value);
			}
		}

		public static readonly DependencyProperty IsPrimarySelectedProperty =
			DependencyProperty.Register("IsPrimarySelected", typeof(bool), typeof(ColorSwitch), new PropertyMetadata(true));
	}
}
